<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_kaoshi/model')->方法名();
*/
namespace jy_ailiao;
defined('BY_JYA') or exit('error');
class kefu extends \plugin{
	public function gettcp($tcpid=0,$tomid=0,$workerid=0,$isfan=0){
		if(!$tcpid){
			if($tomid==$_SESSION['uid']){
				return 0;
			}
			$item=pdo_get('jy_ailiao_tcp',array('tomid'=>$isfan?$_SESSION['uid']:$tomid,'workerid'=>$workerid,'mid'=>$isfan?$tomid:$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
			if(!$item['id']){
				$item=pdo_get('jy_ailiao_tcp',array('mid'=>$isfan?$_SESSION['uid']:$tomid,'workerid'=>$workerid,'tomid'=>$isfan?$tomid:$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
			}
			if(!$item['id']){
				$item=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$isfan?$tomid:$_SESSION['uid'],
					'tomid'=>$isfan?$_SESSION['uid']:$tomid,
					'workerid'=>$workerid,
					'createtime'=>TIMESTAMP,
				);
				pdo_insert('jy_ailiao_tcp',$item);
				$item['id']=pdo_insertid();
			}
		}else{
			$item=pdo_get('jy_ailiao_tcp',array('uniacid'=>$_SESSION['uniacid'],'id'=>$tcpid));
		}
		return $item;
	}
	public function addchatlog($postdata=array(),$isfan=0){
		$chatmsg = trim($postdata['msg']);
		$type=$postdata['type'];
		if(empty($postdata['tcpid'])){
			$return['error'] = 1;
			$return['msg'] = '会话丢失！';
			exi($return);
		}
		if(empty($_SESSION['uid'])){
			$return['error'] = 1;
			$return['msg'] = '请先登录会员！';
			exi($return['msg'],'error');
		}
		if($type==4){
			$chatmsg=$postdata['image'];
		}
		if($type==7){
			$chatmsg=$postdata['file'];
		}
		if($type==8){
			$chatmsg=$postdata['goodsid'];
		}
		if(empty($chatmsg)){
			$return['error'] = 1;
			$return['msg'] = '请输入对话内容！';
			exi($return['msg'],'error');
		}
		if($type == 1){
			//预留
		}elseif($type == 3){
			
			//语言
		}elseif($type == 4){
			//图片
		}elseif($type == 7){
			//文件
				$fileurl=tomedia($postdata['msg']);
				$pathinfo=pathinfo($postdata['msg']);
				$fileicon=$pathinfo[extension];
				$filename=pdo_getcolumn('core_attachment',array('uniacid'=>$_SESSION['uniacid'],'attachment'=>$chatmsg),'title');
				$filename=$filename.'.'.$pathinfo[extension];
				if(strpos($fileicon,'xls')){
					$fileicon='icon-exl';
				}elseif(strpos($fileicon,'pd')){
					$fileicon='icon-pdf';
				}elseif(strpos($fileicon,'do')){
					$fileicon='icon-exl';
				}elseif(strpos($fileicon,'zip') || strpos($fileicon,'rar')){
					$fileicon='icon-zip';
				}else{
					$fileicon='icon-copy';
				}
				$filesize=filesize(ROOT_AT.'/'.$postdata['msg']);
				$filesize=price($filesize/1024,'int');
				$filesize .='kb';
				$fileinfo['filename']=$filename;
				$fileinfo['fileurl']=$fileurl;
				$fileinfo['filesize']=$filesize;
				$fileinfo['fileicon']=$fileicon;
		}elseif($type == 8){
			//礼品
			$goods=pdo_get('jy_ailiao_liping',array('id'=>$chatmsg,'uniacid'=>$_SESSION['uniacid']),array('id','title','price','jifen','picurl'));
		}
		$tcp=pdo_get('jy_ailiao_tcp',array('uniacid'=>$_SESSION['uniacid'],'id'=>$postdata['tcpid']));
		$postdata['tomid']=$tcp['mid']!=$_SESSION['uid']?$tcp['mid']:$tcp['tomid'];
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$isfan?$postdata['tomid']:$_SESSION['uid'],
			'tcpid'=>$postdata['tcpid'],
			'liao_bi'=>$type!=9?$postdata['liao_bi']:0,
			'msg'=>$chatmsg,
			'type'=>$type,
			'goods'=>ijson_encode($goods),
			'fileinfo'=>ijson_encode($fileinfo),
			'tomid'=>$isfan?$_SESSION['uid']:$postdata['tomid'],
			'createtime'=>TIMESTAMP
		);
		if($postdata['audiotime']){
			$data['audiotime'] = intval($postdata['audiotime']/1000);
		}
		pdo_insert('jy_ailiao_tcplog',$data);
		$logid = pdo_insertid();
		if($type==9){
			pdo_update('jy_ailiao_tcp',array('logid'=>$logid),array('id'=>$data['tcpid'],'uniacid'=>$_SESSION['uniacid']));
		}
		$res['log']=array(
			'id'=>$logid,
			'mid'=>$isfan?$postdata['tomid']:$_SESSION['uid'],
			'type'=>$type,
			'goods'=>$goods,
			'fileinfo'=>$fileinfo,
			'tcpid'=>$postdata['tcpid'],
			'liao_bi'=>$type!=9?$postdata['liao_bi']:0,
			'tomid'=>$isfan?$_SESSION['uid']:$postdata['tomid'],
			'msg'=>$chatmsg,
		);
		$res['log']['audioleng'] = $data['audiotime']>2?$data['audiotime']:2;
		return $res;
	}
	public function videocall($tomid=0,$endcall=0,$status=0){
		$item=pdo_get('jy_ailiao_tcp',array('tomid'=>$tomid,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		if(!$item['id']){
			$item=pdo_get('jy_ailiao_tcp',array('mid'=>$tomid,'tomid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		}
		if(!$item['id']){
			return '';
		}
		//挂断
		if($endcall){
			//print_r($item);exit;
			pdo_update('jy_ailiao_tcp',array('status'=>0,'endtime'=>time()),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
			model('jy_videocall/kefu')->videocall($tomid,1);
			$status=0;
			if($item['calltime'] && $item['status']==2){
				if($this->setting['liao_videobi']){
					$fennum=(time()-$item['calltime'])/60;
					$fennum=intval($fennum);
					$price=$fennum*$this->setting['liao_videobi'];
					if($price>0){
						$isnan=pdo_getcolumn('jy_ailiao_member',array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),'sex');
						$nanmid=$isnan==1?$item['mid']:$item['tomid'];
						$member=model('core/member')->detail($nanmid,array('credit1','credit2','credit3','mobile','avatar','nickname'));
						if($member['credit3']<$price){
							return $status;
						}
						model('core/member')->member_credit($nanmid,'credit3',$price,false,'jy_ailiao','男生视频聊天按分钟收费');
						if($item['logid']){
							$tcplog=pdo_get('jy_ailiao_tcplog',array('id'=>$item['logid'],'uniacid'=>$_SESSION['uniacid']));
							if($nanmid==$tcplog['tomid']){
								$data=array(
									'uniacid'=>$_SESSION['uniacid'],
									'mid'=>$nanmid,
									'tcpid'=>$tcplog['tcpid'],
									'liao_bi'=>$price,
									'msg'=>'视频聊天结束',
									'type'=>9,
									'tomid'=>$tcplog['mid'],
									'createtime'=>TIMESTAMP
								);
								$data['audiotime'] = time()-$item['calltime'];
								pdo_insert('jy_ailiao_tcplog',$data);
							}else{
								$data=array(
									'uniacid'=>$_SESSION['uniacid'],
									'mid'=>$tcplog['mid'],
									'tcpid'=>$tcplog['tcpid'],
									'liao_bi'=>$price,
									'msg'=>'视频聊天结束',
									'type'=>9,
									'tomid'=>$nanmid,
									'createtime'=>TIMESTAMP
								);
								$data['audiotime'] = time()-$item['calltime'];
								pdo_insert('jy_ailiao_tcplog',$data);
							}
						}
						$this->model('commission')->upliaofee($item,$price);
					}
				}else{
					$data=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$_SESSION['uid'],
						'tcpid'=>$item['id'],
						'msg'=>'视频聊天结束',
						'type'=>9,
						'tomid'=>$tomid,
						'createtime'=>TIMESTAMP
					);
					$data['audiotime'] = time()-$item['calltime'];
					pdo_insert('jy_ailiao_tcplog',$data);
				}
			}else{
				$data=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$_SESSION['uid'],
						'tcpid'=>$item['id'],
						'msg'=>'关闭视频聊天',
						'type'=>9,
						'tomid'=>$tomid,
						'createtime'=>TIMESTAMP
				);
				$data['audiotime'] = time()-$item['calltime'];
				pdo_insert('jy_ailiao_tcplog',$data);
			}
		}else{
			//拨号中
			if($status==1){
				//file_put_contents(ROOT_D.'/1.txt','拨号中');
				pdo_update('jy_ailiao_tcp',array('status'=>1,'callmid'=>$_SESSION['uid'],'tocalltime'=>time()),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
				$status=1;
			}
			//接听
			if($status==2 && $item['callmid']!=$_SESSION['uid']){
				pdo_update('jy_ailiao_tcp',array('status'=>2,'calltime'=>time()),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
				$status=2;
			}
		}
		return $status;
	}
	public function sendmsg($tcpid=0,$lastid=0){
		$lasttpl=pdo_get('jy_ailiao_tcplog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'tcpid'=>$tcpid),array('tpltime'),'tpltime desc');
		if($lasttpl['tpltime']>time()-300){
			return '';	
		}
		if($this->setting['liao_templateid'] && $this->setting['liao_value1'] && $this->setting['liao_name1'] && $this->setting['liao_value2'] && $this->setting['liao_name2']){
			$log = pdo_get('jy_ailiao_tcplog',array('id'=>$lastid,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'tcpid'=>$tcpid));
			if(!$log['istpl'] && $log['createtime']>time()-300){
				$url=custom_url('jy_ailiao/tcplog//tcpid='.$postdata['tcpid'],true);
				$liao_value1=$this->setting['liao_value1'];
				$worker=$this->model('member')->get_member($log['tomid']);
				$liao_value1=str_replace('{nickname}',$worker['nickname'],$liao_value1);
				$msg=array(
					$this->setting['liao_name1']=>array('value'=>$liao_value1,'name'=>$this->setting['liao_name1'],'title'=>'通知信息1'),
					$this->setting['liao_name2']=>array('value'=>$this->setting['liao_value2'],'name'=>$this->setting['liao_name2'],'title'=>'通知信息2'), 
				);
				cfc('account')->sendTplNotice($log['tomid'], $this->setting['liao_templateid'],$msg,$url);
			}
        }
		return '';
	}
}
?>